package com.pai4j.pgc.service.behavior;
import com.pai4j.common.enums.community.ResourceBehaviorTypeEnum;
import com.pai4j.common.exception.BusinessException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * coderutil资源互动工厂
 */
@Component
public class ResourceBehaviorFactory implements InitializingBean {
    private static final Map<String, AbstractResourceBehaviorService> serviceMap = new HashMap<>();
    @Override
    public void afterPropertiesSet() throws Exception {
        initServiceMap();
    }
    /**
     * 根据 behaviorType 获取 behaviorService
     * @param behaviorType
     * @return
     */
    public static AbstractResourceBehaviorService getBehaviorService(String behaviorType) {
        AbstractResourceBehaviorService behaviorService = serviceMap.get(behaviorType);
        if (behaviorService == null) {
            throw new BusinessException("不支持的behaviorType");
        }
        behaviorService.setBehaviorType(behaviorType);
        return behaviorService;
    }
    /**
     * 注册服务
     */
    private void initServiceMap() {
        // 初始化点赞服务
        serviceMap.put(ResourceBehaviorTypeEnum.LIKE.getBehavior(), new ResourceTwoWayBehaviorService());
        // 星标服务
        serviceMap.put(ResourceBehaviorTypeEnum.STAR.getBehavior(), new ResourceTwoWayBehaviorService());
        // 关注服务
        serviceMap.put(ResourceBehaviorTypeEnum.FOCUS.getBehavior(), new ResourceTwoWayBehaviorService());
        // 收藏服务
        serviceMap.put(ResourceBehaviorTypeEnum.COLLECT.getBehavior(), new ResourceTwoWayBehaviorService());
        // 打赏服务
        serviceMap.put(ResourceBehaviorTypeEnum.REWARD.getBehavior(), new ResourceOneWayBehaviorService());
        // 预览服务
        serviceMap.put(ResourceBehaviorTypeEnum.VIEW.getBehavior(), new ResourceOneWayBehaviorService());
        // 点击服务
        serviceMap.put(ResourceBehaviorTypeEnum.CLICK.getBehavior(), new ResourceOneWayBehaviorService());
        // 分享服务
        serviceMap.put(ResourceBehaviorTypeEnum.SHARE.getBehavior(), new ResourceOneWayBehaviorService());
    }
}
